package com.cqvie.dao;

import java.util.List;

import javax.annotation.Resource;

import com.cqvie.model.Attend;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository("attendDao")
public class AttendDao {
	@Resource
	private SessionFactory sessionFactory;

	// 添加考勤信息
	@Transactional(propagation = Propagation.REQUIRED)
	public void addAttendMessage(Attend at) {
		Session session = sessionFactory.getCurrentSession();
		session.save(at);
	}
	
	public Attend GetAttend(String id){
		Session session=sessionFactory.getCurrentSession();
		Attend attend=(Attend)session.get(Attend.class, id);
		return attend;
	}
	
	//查询所有的考勤
	@SuppressWarnings("unchecked")
	public List<Attend> selectAll() {
		Session session = sessionFactory.getCurrentSession();
		String hql = "from Attend";
		Query query = session.createQuery(hql);
		 List<Attend> attends=query.list();
		if (query.list().size() >= 1) {
			return attends;
		} else
			return null;
	}

	//修改考勤信息
	public boolean updateAttednd(String ID,int absNum,int laterNum){
		String hql="update Attend at set at.absenceNum=? ,at.lateNumber=? where at.ID=?";
		Session session=sessionFactory.getCurrentSession();
		Query query=session.createQuery(hql);
		query.setInteger(0, absNum);
		query.setInteger(1, laterNum);
		query.setString(2, ID);
		int temp=query.executeUpdate();
		if(temp>=1){
			return true;
		}else
			return false;
	}
}
